#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:    御风
# @Mail:      zhong.yufeng@foxmail.com
# @Time:      2022/4/12 1:40


import hashlib
from itsdangerous import URLSafeTimedSerializer


class Serializer(object):

    def __init__(self, secretKey, salt):
        signer = {
            "digest_method":  hashlib.sha256,
            "key_derivation": "hmac",
        }
        self.serializer = URLSafeTimedSerializer(secretKey, salt=salt, signer_kwargs=signer)

    def loads(self, data: str):
        result = { }
        if isinstance(data, str):
            try:
                result = self.serializer.loads(data)
            except Exception:
                pass
        return result

    def dumps(self, data: dict):
        result = ""
        if isinstance(data, dict):
            try:
                result = self.serializer.dumps(data)
            except Exception:
                pass
        return result
